﻿
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class ZToast : MonoBehaviour
{
    private static ZToast instance;
    private static Text textObject;
    public Font font;
    private static float closeTime = 0;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this);
        }
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void FixedUpdate()
    {
        if (closeTime <= Time.fixedTime)
        {
            this.gameObject.SetActive(false);
        }
    }

    public static void Toast(string text, float time = 5)
    {
        if (textObject == null)
        {
            GameObject gameObject = instance == null ? new GameObject("Toast") : instance.gameObject;
            if (instance == null) instance = gameObject.AddComponent<ZToast>();
            Canvas canvas = gameObject.AddComponent<Canvas>();
            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
            canvas.sortingOrder = 99;

            GameObject gameObject1 = new GameObject("Toast", typeof(RectTransform), typeof(Image), typeof(ContentSizeFitter), typeof(HorizontalLayoutGroup));
            gameObject1.transform.SetParent(gameObject.transform);
            gameObject1.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
            HorizontalLayoutGroup horizontal = gameObject1.GetComponent<HorizontalLayoutGroup>();
            horizontal.childScaleWidth = true;
            horizontal.childScaleHeight = true;
            horizontal.padding.right = 15;
            horizontal.padding.left = 15;
            Image image = gameObject1.GetComponent<Image>();
            image.color = new Color(0, 0, 0, 0.5f);
            RectTransform rect = gameObject1.GetComponent<RectTransform>();
            rect.localPosition = Vector3.zero;
            rect.sizeDelta = new Vector2(200, 50);

            GameObject gameObject2 = new GameObject("Text", typeof(RectTransform), typeof(Text));
            gameObject2.transform.SetParent(gameObject1.transform);
            rect = gameObject2.GetComponent<RectTransform>();
            rect.anchorMin = new Vector2(0, 0);
            rect.anchorMax = new Vector2(1, 1);
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;
            textObject = gameObject2.GetComponent<Text>();
            textObject.color = Color.white;
            textObject.alignment = TextAnchor.MiddleCenter;
            textObject.fontSize = Screen.width / 50;
            textObject.resizeTextForBestFit = true;
            textObject.resizeTextMaxSize = 100;
            textObject.resizeTextMinSize = 15;

        }
        if (textObject.font == null)
        {
            if (instance.font != null)
            {
                textObject.font = instance.font;
            }
            else
            {
                Font[] fonts = Resources.LoadAll<Font>("");
                for (int i = 0; i < fonts.Length; i++)
                {
                    if (fonts.ToString() == "SIMHEI")
                    {
                        textObject.font = fonts[i];
                        break;
                    }
                }
                if (textObject.font == null && fonts.Length > 0)
                {
                    textObject.font = fonts[0];
                }
            }
            if (textObject.font == null)
            {
                Debug.LogError("Toast字体为空");
            }
        }
        textObject.text = text;
        closeTime = Time.fixedTime + time;
        instance.gameObject.SetActive(true);
    }

}
